home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 431 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  84 lines

  1. Path: news.microsoft.com!news
  2. From: a-cnadc@microsoft.com (Dann Corbit)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Array Parameters
  5. Date: 4 Jan 1996 21:55:12 GMT
  6. Organization: Microsoft Corporation
  7. Message-ID: <4chic0$mkd@news.microsoft.com>
  8. References: <wayne.820650643@hawk> <4ce349$4j9@hacgate2.hac.com> <820701694snz@genesis.demon.co.uk> <4cg104$qmp@nervous.pdb.sni.de> <4cgmuc$k9k@hacgate2.hac.com>
  9. NNTP-Posting-Host: 157.57.171.202
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.93.14
  12.  
  13. In article <4cgmuc$k9k@hacgate2.hac.com>, collins@thor.tu.hac.com says...
  14. >
  15. >Josef Moellers (mollers.pad@sni.de) wrote:
  16. >: In <820701694snz@genesis.demon.co.uk> Lawrence Kirby <fred@genesis.demon.co.uk> writes:
  17. >
  18. >: [ ... ]
  19. >
  20. >: >                                                     It is impossible
  21. >: >to specify an array as a function parameter since, as in the example above,
  22. >
  23. >: Well, if you MUST pass an array by value and not by reference, wrap it
  24. >: into a structure:
  25. >
  26. >: struct foo {
  27. >:     int array[100];
  28. >: };
  29. >: ...
  30. >
  31. >
  32. >I don't get it ... what does this buy me ("me" == any competant C programmer)?
  33. >I'm still going to get a pointer-to-something, whether it's a pointer-to-
  34. >structure, pointer-to-array, or pointer-to-first-element.  I still need to
  35. >know which one is used so I can access the array elements properly.  (Although,
  36. >there is seldom any difference between passing a pointer to an array, and
  37. >a pointer to the first element of that array).
  38. >
  39. >BTW, passing a pointer to an object is _not_ the same as pass by reference.
  40. >C does not support pass-by-reference at all.
  41. If you pass the struct by value you will get a copy of the whole structure.
  42. This means that:
  43. 1.  All sizeof( struct foo ) bytes are piled onto the stack
  44. 2.  You can change the contents of the array in the function without 
  45.     changing the contents of the array in the calling program.
  46.  
  47. #include <stdio.h>
  48. #include <string.h>
  49. typedef struct foo {
  50.    char pszData[100];
  51. } footype;
  52. int iDontClobberTheFoo( footype );
  53. int main()
  54. {
  55.    footype myFoo;
  56.    strcpy(myFoo.pszData, "Hi there, my name is skinner the grinner");
  57.    puts("Foo before call");
  58.    puts( myFoo.pszData );
  59.    iDontClobberTheFoo( myFoo );
  60.    puts("Foo after call");
  61.    puts( myFoo.pszData );
  62.    return 0;
  63. }
  64. int iDontClobberTheFoo( footype aFooCopy )
  65. {
  66.    strcpy(aFooCopy.pszData, "This data is completely different");
  67.    puts("Foo during call");
  68.    puts( aFooCopy.pszData );
  69.    return 0;
  70. }
  71. /*  The program above outputs the following on my compiler:
  72. Foo before call
  73. Hi there, my name is skinner the grinner
  74. Foo during call
  75. This data is completely different
  76. Foo after call
  77. Hi there, my name is skinner the grinner
  78. */
  79.  
  80. -- 
  81. The opinions expressed in this message are my own personal views 
  82. and do not reflect the official views of Microsoft Corporation.
  83.  
  84.